package com.shaoshaohuo.app.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/6/20.
 */

public class FiveCornerStar extends LinearLayout {

    private int i1;
    private int numstart;

    public int getNumstart() {
        return numstart;
    }

    public FiveCornerStar(Context context) {
        this(context, null);
    }

    public FiveCornerStar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FiveCornerStar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private int num;

    public void setNum( int num) {
        removeAllViews();
        this.num = num;
        Log.e("TAG", "setNum: "+num );
        for (int i = 0; i < num; i++) {
            MyStarView myview = new MyStarView(getContext());
            addView(myview);

        }
        post(new Runnable() {
            @Override
            public void run() {
                if (FiveCornerStar.this.num != 0) {
                    i1 = getWidth() / FiveCornerStar.this.num;
                }
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    LayoutParams layoutParams = new LayoutParams(i1, getHeight());
                    childAt.setLayoutParams(layoutParams);
                }
            }
        });


    }
    public void setNum(int num, final int width, final int height) {
        removeAllViews();
        this.num = num;
        Log.e("TAG", "setNum: "+num );
        for (int i = 0; i < num; i++) {
            MyStarView myview = new MyStarView(getContext());
            addView(myview);

        }
        post(new Runnable() {
            @Override
            public void run() {
                if (FiveCornerStar.this.num != 0) {
                    i1 = getWidth() / FiveCornerStar.this.num;
                }
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    LayoutParams layoutParams = new LayoutParams(width, height);
                    childAt.setLayoutParams(layoutParams);
                }
            }
        });


    }
    private void init() {
        setOrientation(HORIZONTAL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    public interface OnCallBackListener {
        void onItemClickListener(View view, float num);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isdown) {
            float startnum = 0;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    float x = event.getX();
                    //i1是控件宽度 ，x:0-getwidth();
//                float v = x % i1;
//                if(v>0){
//                    v=1;
//                }
                    for (int i = 0; i < getChildCount(); i++) {
                        MyStarView childAt = (MyStarView) getChildAt(i);
                        childAt.setRw(0);
                    }
                    float v1 = x / i1;
                    float v2 = v1;
                    startnum = v2;
                    if (getChildCount() >= v2) {
                        for (int i = 0; i < v2; i++) {
                            MyStarView childAt = (MyStarView) getChildAt(i);
                            childAt.setRw(childAt.getWidth());
                        }
                    } else {
                        for (int i = 0; i < getChildCount(); i++) {
                            MyStarView childAt = (MyStarView) getChildAt(i);
                            childAt.setRw(childAt.getWidth());
                        }
                        startnum = getChildCount();
                    }
                    if (onCallBackListener != null) {
                        onCallBackListener.onItemClickListener(this, startnum);
                    }
                    break;
            }
        }

        return super.onTouchEvent(event);
    }


    private boolean isdown = true;

    public void setIsdown(boolean isdown) {
        this.isdown = isdown;
    }

    public void setdownnum(final float num) {
        post(new Runnable() {
            @Override
            public void run() {
                if (num > getChildCount()) {
                    for (int i = 0; i < getChildCount(); i++) {
                        MyStarView childAt = (MyStarView) getChildAt(i);
                        childAt.setRw(i1);
                    }
                } else {
                    double rint = Math.rint(num);
                    if (rint == num) {
                        //不是小数量
                        for (int i = 0; i < num; i++) {
                            MyStarView childAt = (MyStarView) getChildAt(i);
                            childAt.setRw(i1);
                        }
                    } else {
                        int a = (int) num;
                        float v = num - a;
                        for (int i = 0; i < a; i++) {
                            MyStarView childAt = (MyStarView) getChildAt(i);
                            childAt.setRw(i1);
                        }
                        MyStarView childAt = (MyStarView) getChildAt(a);
                        childAt.setRw(v * i1);
                    }

                }
            }
        });

    }
}
